package com.xty.xtyle.frames;

import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.prolificinteractive.materialcalendarview.CalendarDay;
import com.prolificinteractive.materialcalendarview.MaterialCalendarView;
import com.prolificinteractive.materialcalendarview.OnDateSelectedListener;
import com.xty.xtyle.R;
import com.xty.xtyle.base.AbsFragment;
import com.xty.xtyle.calendar.EventDecorator;
import com.xty.xtyle.db.mix.MixCloth;
import com.xty.xtyle.db.mix.MixDao;
import com.xty.xtyle.event.FreshMixEvent;
import com.xty.xtyle.mix.MixDetialActivity;
import com.xty.xtyle.utils.CommonUtil;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collection;
import java.util.Date;
import java.util.List;

/**
 * 日历界面
 */
public class CanFragment extends AbsFragment {
    MaterialCalendarView calendarView;
    MixDao mixDao = new MixDao();
    @Override
    public int getLayoutId() {
        return R.layout.fragment_can;
    }

    @Override
    public void initView() {
        if (!EventBus.getDefault().isRegistered(this))
            EventBus.getDefault().register(this);
        calendarView = findViewById(R.id.calendarView);
        load();
    }

    @Override
    public void freshData() {

    }

    @Subscribe
    public void onEvent(FreshMixEvent result) {
        //接收以及处理数据
        load();
    };

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
    /**
     * 加载混搭衣服，然后添加到日历空间上面：
     */
    void load() {
        List<MixCloth> mixes = mixDao.queryAll();
        if (null == mixes || mixes.isEmpty()) return;
        Collection<CalendarDay> dates=new ArrayList<>();
        Calendar calendar = Calendar.getInstance();
        for (MixCloth mixCloth : mixes) {
            try {
                calendar.setTime(new Date(Long.valueOf(mixCloth.getTime())));
                int year = calendar.get(Calendar.YEAR);
                int month = calendar.get(Calendar.MONTH) + 1;
                int day = calendar.get(Calendar.DAY_OF_MONTH);
//            CommonUtil.showToast(year+"-"+month+"-"+day);
                dates.add(new CalendarDay(CommonUtil.passDate2Date(year + "-" + month + "-" + day)));
            }catch (Exception e) {

            }
        }
        calendarView.addDecorator(new EventDecorator(Color.RED, dates));
        //监听日历点击事件，如果这个时间有混搭，进入混搭详细页面
        calendarView.setOnDateChangedListener(new OnDateSelectedListener() {
            @Override
            public void onDateSelected(@NonNull MaterialCalendarView widget, @NonNull CalendarDay date, boolean selected) {
                if (selected) {
                    long time = date.getDate().getTime();
                    List<MixCloth> cloths = mixDao.queryByTime(time);
                    if (null != cloths && cloths.size()>0) {
                        MixDetialActivity.startActivity(contentView.getContext(), cloths.get(0).getId());
                    }
                }
            }
        });
    }
}
